﻿using Emgu.CV;
using Emgu.CV.Structure;
using GameTools.Classes;
using Loli.Library.Util;
using Loli.Library.WinIo;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameTools.Models
{
    [Serializable]
    public class Player
    {
        public Player()
        {
            this.Skills = new List<Skill>();
            this.Life = MapleStory.GetLifeBar();
            this.Magic = MapleStory.GetMagicBar();
        }



        [NonSerialized]
        private Image<Gray, byte> _Mark1 = null;

        public Image<Gray, byte> Mark1
        {
            get { return _Mark1; }
            set { _Mark1 = value; }
        }




        [NonSerialized]
        private Image<Gray, byte> _Mark2 = null;

        public Image<Gray, byte> Mark2
        {
            get { return _Mark2; }
            set { _Mark2 = value; }
        }



        //标识1
        public string MarkName1 { get; set; }
        //标识2
        public string MarkName2 { get; set; }

        public string Name { get; set; }
        public int Level { get; set; }
        public Jobs Job { get; set; }


        public LifeAndMagic Life { get; set; }
        public LifeAndMagic Magic { get; set; }
        public List<Skill> Skills { get; set; }

        public int SkillRange { get; set; }
        public int SkillDirection { get; set; }


        public void Save()
        {
            string filePath = Players.DIRECTORY_PATH + "\\" + this.Name + ".ini";
            LoliCode.Serialization<Player>(this, filePath);
        }

    }

    [Serializable]
    public class Players : List<Player>
    {
        public const string DIRECTORY_PATH = "Players";
        public const string POINT_FILE_PATH = @"Datas\Point.bin";

        [NonSerialized]
        private Image<Bgr, byte> _Point = null;

        public Image<Bgr, byte> Point
        {
            get { return _Point; }
            set { _Point = value; }
        }

        public static Players Load()
        {
            Players players = new Players();

            if (!Directory.Exists(DIRECTORY_PATH))
            {
                Directory.CreateDirectory(DIRECTORY_PATH);
            }

            foreach (var item in Directory.GetFiles(DIRECTORY_PATH))
            {
                try
                {
                    Player player = LoliCode.Deserialization<Player>(item);
                    
                    if (player.Skills != null)
                    {
                        for (int i = 0; i < player.Skills.Count; i++)
                        {
                            player.Skills[i].ID = i;
                        }
                    }
                    players.Add(player);
                }
                catch { }
            }

            players.Point = new Image<Bgr, byte>(POINT_FILE_PATH);

            return players;

        }

        public void Save()
        {

            if (!Directory.Exists(DIRECTORY_PATH))
            {
                Directory.CreateDirectory(DIRECTORY_PATH);
            }

            foreach (var item in Directory.GetFiles(DIRECTORY_PATH))
            {
                File.Delete(item);
            }

            foreach (var item in this)
            {
                item.Save();
            }
        }

    }
}
